import React, {Component} from 'react';
import {ToDo} from "../ToDoList";
import {Guid} from "guid-typescript";

type IProps = {
    addToDo: any
}

class Header extends Component<IProps> {

    constructor(props: IProps) {
        super(props);
    }

    handleKeyPress = (event: any) => {
        if (event.charCode === 13) {
            if (event.target.value === '') return
            let todo = new ToDo(Guid.create().toString(), event.target.value);
            let {addToDo} = this.props;
            addToDo(todo)
            event.target.value = ''
        }

    }


    render() {

        return (
            <div>
                <input type={"text"} onKeyPress={this.handleKeyPress}/>
            </div>
        );
    }
}


export default Header;
